from datetime import datetime
from django.db import models
from django.contrib.auth.models import User 
from thoughts.models import Thought

class ThoughtLink(models.Model):
    shared_thought = models.ForeignKey(Thought) 
    from_author   = models.ForeignKey(User, related_name = "following_set")
    to_author     = models.ForeignKey(User, related_name = "follower_set")
    
    date_added = models.DateTimeField(default= datetime.now) 
    
    def __unicode__(self):
        return "%s is sharing (%s) with %s" % (self.from_author.username, 
            self.shared_thought.title, self.to_author.username)

    def save(self, **kwargs):
        if self.from_author == self.to_user:
            raise ValueError(" You cannot share with yourself ")
        super(ThoughtLink, self).save(**kwargs)
    
    class Meta:
        unique_together = (('shared_thought', 'from_author', 'to_author'),) 
